﻿namespace CoreClass
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CheckName
    {
        protected DBCommon dbc = new DBCommon();
        protected string FirstConn;
        private string stringConn;
        private string stringTable;
        private string tempID2;
        private string tempID3;
        private string tempName;

        public CheckName(string ID2, string ID3, string Name)
        {
            this.FirstConn = this.dbc.strConnectstring;
            this.tempName = Name;
            this.tempID2 = ID2;
            this.tempID3 = ID3;
            this.First();
        }

        public void First()
        {
            SqlCommand mySqlCommand = this.dbc.GetCommandBySql("select x1 server,x2 Data,x3 uid,x4 pwd,x5 biao from d_a where ID=" + this.tempID3);
            mySqlCommand.Connection.Open();
            SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                while (mySqlDataReader.Read())
                {
                    this.stringConn = "server=" + mySqlDataReader.GetValue(0).ToString() + ";User ID=" + mySqlDataReader.GetValue(2).ToString() + ";Password=" + mySqlDataReader.GetValue(3).ToString() + ";database=" + mySqlDataReader.GetValue(1).ToString() + ";Connection Reset=FALSE";
                    this.stringTable = mySqlDataReader.GetValue(4).ToString();
                }
            }
            finally
            {
                mySqlDataReader.Close();
            }
        }

        public bool ValidateName()
        {
            this.First();
            SqlCommand mySqlCommand = this.dbc.GetCommandBySql("select name,id2 from " + this.stringTable.ToString() + " where name='" + this.tempName + "' and  ID2=" + this.tempID2.ToString());
            mySqlCommand.Connection.Open();
            SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                if (mySqlDataReader.Read())
                {
                    return false;
                }
            }
            finally
            {
                mySqlDataReader.Close();
            }
            return true;
        }

        public string RecordConn
        {
            get
            {
                return this.stringConn;
            }
        }

        public string TableName
        {
            get
            {
                return this.stringTable;
            }
        }
    }
}

